<%-- 
    Document   : AddPerson
    Created on : May 3, 2013, 8:00:38 PM
    Author     : baumae03
--%>

<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%

if(session.getAttribute("UserId") == null) {
    response.sendRedirect("index.jsp");
}

if((Boolean)session.getAttribute("IsAdmin") == false) {
    response.sendRedirect("Home.jsp");
}

if (request.getParameter("submit") == null) {
%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add Person</title>
        <link rel="stylesheet" type="text/css" href="Scripts/addperson.css"/>
    </head>
    <body>
    <Div style="text-align:right;background-color:#F0F0F0;" id="TopMenuBar" >
		<%
			out.print("<span id='WelcomSpan'>Welcome " + session.getAttribute("Username") + "  </span>");
			out.print("<a href='index.jsp'><button>Logout</button></a>");
			if((Boolean)session.getAttribute("IsAdmin")){
				out.print("<a href='Admin.jsp'><button>Admin</button></a>");
			               out.print("<a href='AddPerson.jsp'><button>Add Person</button></a>");
			}
		%>
		</Div>
    
    <center>
        <form name="InsertPerson" method="POST" action="${pageContext.request.contextPath}/AddPerson.jsp">
        <table>
            <tr>
                <th>
                    Field
                </th>
                <th>
                    Value
                </th>
            </tr>
            <tr>
                <td class="formLabel">
                    Name
                </td>
                <td>
                    <input type="text" name="name" />
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Gender
                </td>
                <td>
                    <select name="gender">
                        <option value="1">Male</option>
                        <option value="0">Female</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Birth Date (YYYY-MM-DD)
                </td>
                <td>
                    <input type="text" name="birthdate" />
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Birth Place
                </td>
                <td>
                    <input type="text" name="birthplace" />
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Biography
                </td>
                <td>
                    <textarea cols="40" rows="20" name="bio"></textarea>
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Last Known Address
                </td>
                <td>
                    <input type="text" name="lastknownaddress" />
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Death Date (YYYY-MM-DD)
                </td>
                <td>
                    <input type="text" name="deathdate" />
                </td>
            </tr>
            <tr>
                <td class="formLabel">
                    Death Place
                </td>
                <td>
                    <input type="text" name="deathplace" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <input name="submit" type="submit" value="Submit" />
                </td>
            </tr>
        </table>
        </form>
    </center>
    </body>
</html>
<%
} else {

Connection con = null;
int personId = -1;
try {
    con = DriverManager.getConnection("jdbc:mysql://webdev.cs.uwosh.edu:4381/severa24", "severa24", "d450224");
    
    String SQLStatement = "INSERT INTO Person ( ";
    int valueCount = 0;
    if(request.getParameter("bio").length() > 0) {
        SQLStatement += "Bio,";
        valueCount++;
    }
    if(request.getParameter("birthdate").length() > 0) {
        SQLStatement += "BirthDate,";
        valueCount++;
    }
    if(request.getParameter("birthplace").length() > 0) {
        SQLStatement += "BirthPlace,";
        valueCount++;
    }
    if(request.getParameter("name").length() > 0) {
        SQLStatement += "Name,";
        valueCount++;
    }
    if(request.getParameter("gender").length() > 0) {
        SQLStatement += "Gender,";
        valueCount++;
    }
    if(request.getParameter("lastknownaddress").length() > 0) {
        SQLStatement += "LastKnownAddress,";
        valueCount++;
    }
    if(request.getParameter("deathdate").length() > 0) {
        SQLStatement += "DeathDate,";
        valueCount++;
    }
    if(request.getParameter("deathplace").length() > 0) {
        SQLStatement += "DeathPlace,";
        valueCount++;
    }
    SQLStatement = SQLStatement.substring(0, SQLStatement.length() - 1);
    SQLStatement += ") VALUES (";
    for(int i = 0; i < valueCount -1; i++) {
        SQLStatement += "?, "; 
    }
    SQLStatement += "?)";
    PreparedStatement insertPerson = con.prepareStatement(SQLStatement, PreparedStatement.RETURN_GENERATED_KEYS);

       int insertIncrement = 1;
    
    if(request.getParameter("bio").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("bio"));
        insertIncrement++;
    }
    if(request.getParameter("birthdate").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("birthdate"));
        insertIncrement++;
    }
    if(request.getParameter("birthplace").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("birthplace"));
        insertIncrement++;
    }
    if(request.getParameter("name").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("name"));
        insertIncrement++;
    }
    if(request.getParameter("gender").length() > 0) {
        insertPerson.setInt(insertIncrement, Integer.parseInt(request.getParameter("gender")));
        insertIncrement++;
    }
    if(request.getParameter("lastknownaddress").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("lastknownaddress"));
        insertIncrement++;
    }
    if(request.getParameter("deathdate").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("deathdate"));
        insertIncrement++;
    }
    if(request.getParameter("deathplace").length() > 0) {
        insertPerson.setString(insertIncrement, request.getParameter("deathplace"));
        insertIncrement++;
    }

    if(insertPerson.executeUpdate() > 0) {
        ResultSet rst = insertPerson.getGeneratedKeys();
        rst.next();
        personId = rst.getInt(1);
    }
     
} catch (SQLException ex) {
    System.err.println(ex);
} finally {
    if (con != null) {
        try{
            con.close();
        } catch (SQLException ex) {
            System.err.println(ex);
        }
    }
}

if(personId != -1) {
    response.sendRedirect("ViewIndividual.jsp?PersonId=" + personId);
} else {
    response.sendError(500, "There was an error inserting the person into the database.");
}

}
    
%>